import { delOss, listOss } from '@/api/system/oss'
import { addDateRange, formatDate } from '@/utils/common'
import { http } from '@/utils/http'
import { message } from '@/utils/message'
import type { PaginationProps } from '@pureadmin/table'
import { type DateModelType, ElMessageBox } from 'element-plus'
import { onMounted, reactive, ref, toRefs } from 'vue'

export function useOSS() {
  const data = reactive({
    form: {},
    queryParams: {
      url: undefined,
      fileName: undefined,
      originalName: undefined,
      fileSuffix: undefined,
      createTime: undefined,
      createBy: undefined,
      service: undefined,
    },
  })
  const { form, queryParams } = toRefs(data)
  const operId = ref(0)
  const title = ref('')
  const ids = ref([])
  const single = ref(true)
  const multiple = ref(true)
  const dataList = ref([])
  const loading = ref(true)
  const showSelection = true
  const dateRange = ref<DateModelType>(null)
  const formDialogVisible = ref(false)
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true,
  })
  const columns: TableColumnList = [
    {
      type: 'selection',
      width: 55,
      align: 'left',
      hide: ({ checkList }) => !checkList.includes('勾选列'),
    },
    {
      label: '序号',
      type: 'index',
      width: 70,
      hide: ({ checkList }) => !checkList.includes('序号列'),
    },
    {
      label: '对象存储主键',
      prop: 'ossId',
      minWidth: 80,
    },
    {
      label: '文件名',
      prop: 'fileName',
      minWidth: 120,
    },
    {
      label: '原名',
      prop: 'originalName',
      minWidth: 120,
    },
    {
      label: '文件后缀',
      prop: 'fileSuffix',
      minWidth: 80,
    },
    {
      label: '文件展示',
      prop: 'url',
      minWidth: 150,
      cellRenderer: (scope) => {
        return (
          <el-image
            class={'h-[60px] w-[60px] rounded-md'}
            src={scope.row.url}
            previewSrcList={[scope.row.url]}
            previewTeleported={true}
          ></el-image>
        )
      },
    },
    {
      label: '上传人',
      prop: 'createBy',
      minWidth: 100,
    },
    {
      label: '服务商',
      prop: 'service',
      minWidth: 100,
    },
    {
      label: '创建时间',
      minWidth: 180,
      prop: 'createTime',
      formatter: ({ operTime }) => formatDate(operTime),
    },
    {
      label: '操作',
      fixed: 'right',
      width: 150,
      slot: 'operation',
    },
  ]

  /** 文件按钮操作 */
  function handleFile() {
    message('上传文件施工中', { type: 'success' })
  }

  /** 图片按钮操作 */
  function handleImage() {
    message('上传图片施工中', { type: 'success' })
  }

  /** 下载按钮操作 */
  function handleDownload(row) {
    http.oss(row.ossId, row.fileName)
  }

  /** 删除按钮操作 */
  function handleDelete(row) {
    const ossIds = row.ossId || this.ids
    ElMessageBox.confirm('是否确认删除OSS对象存储编号为"' + ossIds + '"的数据项?')
      .then(() => {
        loading.value = true
        return delOss(ossIds)
      })
      .then(() => {
        loading.value = false
        message('删除成功', { type: 'success' })
        onSearch()
      })
      .finally(() => {
        loading.value = false
      })
  }

  /** 改变分页大小 */
  function handleSizeChange(val: number) {
    pagination.pageSize = val
    onSearch()
  }

  /** 改变当前分页 */
  function handleCurrentChange(val: number) {
    pagination.currentPage = val
    onSearch()
  }

  /** 改变多选项 */
  function handleSelectionChange(selection) {
    ids.value = selection.map((item) => item.operId)
    single.value = selection.length != 1
    multiple.value = !selection.length
  }

  /** 搜索按钮操作 */
  async function onSearch() {
    loading.value = true
    const params = {
      ...queryParams.value,
      pageNum: pagination.currentPage,
      pageSize: pagination.pageSize,
    }
    const { rows, total } = await listOss(addDateRange(params, dateRange.value))
    dataList.value = rows
    pagination.total = total
    loading.value = false
  }

  /** 重置按钮操作 */
  const resetForm = (formEl) => {
    if (!formEl) return
    formEl.resetFields()
    dateRange.value = null
    onSearch()
  }

  onMounted(() => {
    onSearch()
  })

  return {
    form,
    title,
    operId,
    loading,
    columns,
    dataList,
    multiple,
    dateRange,
    pagination,
    queryParams,
    showSelection,
    formDialogVisible,
    onSearch,
    resetForm,
    handleFile,
    handleImage,
    handleDelete,
    handleDownload,
    handleSizeChange,
    handleCurrentChange,
    handleSelectionChange,
  }
}
